package com.wanpinghui.supplier;

import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;

import com.githang.statusbar.StatusBarCompat;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.oauth.LoginActivity;
import com.wanpinghui.supplier.oauth.LoginUtil;
import com.wanpinghui.supplier.object.HomePageActivity;
import com.wanpinghui.wphlibrary.cameralibrary.util.LogUtil;
import com.yunshl.yunshllibrary.manager.MActivityManager;
import com.yunshl.yunshllibrary.utils.LogUtils;
import com.yunshl.yunshllibrary.utils.SystemUtil;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


@ContentView(R.layout.layout_activity_start)
public class StartActivity extends BaseActivity {
    private Subscription subscription;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {


    }

    @Override
    public void bindEvents() {
        LogUtils.w("屏幕宽:" + DensityUtil.getScreenWidth() + "==========屏幕高:" + DensityUtil.getScreenHeight());
        LogUtils.w("屏幕密度:" + DensityUtil.getDensity());
    }

    @Override
    public void initData() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimaryBlue), true);
            } else {
                if (SystemUtil.checkIsMeizuRom() || SystemUtil.isMIUI()) {
                    StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimaryBlue), true);
                } else {
                    StatusBarCompat.setLightStatusBar(getWindow(), false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //   StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimaryBlue), false);
        subscription = Observable.timer(3000, TimeUnit.MILLISECONDS, Schedulers.io())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        if (LoginUtil.isLogin()) {
                            MActivityManager.getInstance().delACT(HomePageActivity.class);
                            startActivity(new Intent(StartActivity.this, HomePageActivity.class));
                            finish();
                        }else {
                            Intent intent = new Intent(StartActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (LoginUtil.isLogin()) {
                            MActivityManager.getInstance().delACT(HomePageActivity.class);
                            startActivity(new Intent(StartActivity.this, HomePageActivity.class));
                            finish();
                        }else {
                            Intent intent = new Intent(StartActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }


}
